function Task() {
	this.id = (new Date()).getTime();
	this.label = "";
	this.description = "";
};
	
function Person() {
	this.id = (new Date()).getTime();
	this.firstName = "";
	this.lastName = "";
	this.tasks = {};
	this.loadTasks = function() {
		
		try {
			Ajax.tasks();
		} catch(exception) {
			console.log('error loading' + exception);
		}	
		
	}
	this.addTask = function(task) {
		this.tasks[task.id] = task;
		
		try {
			Ajax.addTask(task);
		} catch(exception) {
			console.log('error loading' + exception);
		}
	}
	this.updateTask = function(task) {
		
		try {
			Ajax.updateTask(task);
		} catch(exception) {
			console.log('error loading' + exception);
		}
	}
	this.closeTask = function(task) {
		
		try {
			Ajax.closeTask(task.id);
		} catch(exception) {
			console.log('error loading' + exception);
		}
		
		delete this.tasks[task.id];
	}
};
	
function ListPersons() {
	this.persons = {};
	this.load = function() {
		
		try {
			Ajax.persons();
		} catch(exception) {
			console.log('error loading' + exception);
		}
	}
	this.add = function(person) {
		this.persons[person.id] = person;
		
		try {
			Ajax.addPerson(person);
		} catch(exception) {
			console.log('error loading' + exception);
		}
	}
	this.remove = function(id) {
		var person = delete this.persons[id];
		
		try {
			Ajax.removePerson(id);
		} catch(exception) {
			console.log('error loading' + exception);
		}
		
		delete this.persons[id];
	}
};

//Init context
Context = new Object();
Context.listPersons = new ListPersons();
Context.currentPerson = null;
Context.currentTask = null;
Context.updateListPersons = function() {
	
	var ihmList = $('#listPersons');
	ihmList.empty();
	//model -> ihm
	for(var key in Context.listPersons.persons) {
		var person = Context.listPersons.persons[key];
		ihmList.append('<li id="'+person.id+'"><a href="javascript:Context.consultPerson('+person.id+')">'+person.firstName+' '+person.lastName+'</a><a href="javascript:Context.trashPerson('+person.id+')"></a></li>');
	}
	ihmList.listview('refresh');
};
Context.updateListTasks = function() {
	var ihmList = $('#listTasks');
	ihmList.empty();
	//model -> ihm
	for(var key in Context.currentPerson.tasks) {
		var task = Context.currentPerson.tasks[key];
		ihmList.append('<li id="'+task.id+'"><a href="javascript:Context.consultTask('+task.id+')">'+task.label+'</a></li>');
	}
	ihmList.listview('refresh');
};
Context.consultPerson = function(id) {
	
	Context.currentPerson = Context.listPersons.persons[id];
	$.mobile.changePage($('#tasks_page'));
};
Context.trashPerson = function(id) {
	
	//model
	Context.listPersons.remove(id);
	//ihm
	$('#'+id).remove();
	$('#listPersons').listview('refresh');
};
Context.submitPerson = function() {
	
	if( $.checkAllRequired('form#form_person') ) {
		//model
		var person = new Person();
		person.firstName = $("#firstName").val();
		person.lastName = $("#lastName").val();
		Context.listPersons.add(person);
		//ihm
		$.mobile.changePage($('#persons_page'));
	}
};
Context.consultTask = function(id) {
	Context.currentTask = Context.currentPerson.tasks[id];
	$.mobile.changePage($('#update_task_page'));
};
Context.submitTask = function() {
	
	if( $.checkAllRequired('form#form_task') ) {
		
		var task = new Task();
		task.label = $('#label').val();
		task.description = $('#description').val();
		Context.currentPerson.addTask(task);
		
		$.mobile.changePage($('#tasks_page'));
	}
};
Context.submitUpdtTask = function() {
	
	if( $.checkAllRequired('form#update_form_task') ) {
		
		var task = Context.currentTask;
		task.label = $('#ulabel').val();
		task.description = $('#udescription').val();
		Context.currentPerson.updateTask(task);
		
		$.mobile.changePage($('#tasks_page'));
	}
};
Context.closeTask =  function(){
	
	Context.currentPerson.closeTask(Context.currentTask);
	$.mobile.changePage($('#tasks_page'));
};
  
Context.load = function() {

  
	$('#persons_page').live('pageshow',function(event, ui){
		
		$('#listTasks').empty();
		Context.listPersons.load();
		Context.currentPerson = null;
		Context.currentTask = null;
		Context.updateListPersons();
	});
		
	// form_person_page events	
	$('#form_person_page').live('pageshow',function(event, ui){
		
		$("#firstName").val('');
  		$("#lastName").val('');
	});
	
	$('#form_person_page').live('pagehide',function(event, ui){
		
		$("#firstName").val('');
  		$("#lastName").val('');
	});
	
	// tasks_page events
	$('#tasks_page').live('pageshow',function(event, ui){
		Context.currentPerson.loadTasks();
		Context.currentTask = null;
		Context.updateListTasks();		
	});
	
	// form_task_page events
	$('#form_task_page').live('pageshow',function(event, ui){
		
		$('#label').val('');
		$('#description').val('');
	});
	
	$('#form_task_page').live('pagehide',function(event, ui){
		
		$('#label').val('');
		$('#description').val('');
	});
	
	// update_task_page events
	$('#update_task_page').live('pageshow',function(event, ui){
		
		var task = Context.currentTask;
		$('#ulabel').val(task.label);
		$('#udescription').val(task.description);
	});
	
	$('#update_task_page').live('pagehide',function(event, ui){
		
		$('#ulabel').val('');
		$('#udescription').val('');
	});
	
	$('#update_task_page').live('swiperight',function(event){
		
		var prevId = Context.currentTask.id;
		
    	for(var key in Context.currentPerson.tasks) {
    		if(key == Context.currentTask.id) {
    			break;
    		} else {
    			prevId = key;
    		}
    	}
    	
    	Context.currentTask = Context.currentPerson.tasks[prevId]
    	$('#ulabel').val(Context.currentTask.label);
    	$('#udescription').val(Context.currentTask.description);
		
    });
    
    
    $('#update_task_page').live('swipeleft',function(event){
    	
    	var nextId = Context.currentTask.id;
		
    	var stop = false;
    	for(var key in Context.currentPerson.tasks) {
    		if(stop) {
    			nextId = key;
    			break;
    		} else if(key == Context.currentTask.id) {
    			stop = true;
    		} 
    	}
    	
    	Context.currentTask = Context.currentPerson.tasks[nextId]
    	$('#ulabel').val(Context.currentTask.label);
    	$('#udescription').val(Context.currentTask.description);
    });
	
};